package com.atsmart.app.sql;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.atsmart.app.model.TaskInfo;
import com.atsmart.app.model.TaskListInfo;
import com.atsmart.app.model.TaskTemInfo;

public class TaskTemInfoSqlHelper {

	private SQLiteDatabase db = null;

	/**
	 * 打开数据库
	 */
	public void open(SqlHelper dh) {
		// dh = new SqlHelper(null, "atsmart", null,SqlHelper.getDBVersion());
		db = dh.getWritableDatabase();
	}

	public synchronized void addAttribute(TaskTemInfo at) {
		if (at == null) {
			return;
		}
		String strSql = "insert or replace into task_template_info (task_tem_id,task_id,pattern_id) values(?,?,?);";
		try {
			SQLiteStatement stm = db.compileStatement(strSql);
			stm.bindLong(1, at.getId());
			stm.bindLong(2, at.getTask_id());
			stm.bindLong(3, at.getPattern_id());
			stm.execute();
			stm.clearBindings();
			stm.close();
		} catch (Exception e) {
			// will not happen
		}
	}

	public TaskListInfo getTaskList(int begin, int pagesize) {
		// begin = 2;
		// end= 6;
		String sql = " select date,name,content,state,templateID,polId,lon,lat,template_num,tid from task_info  order by  date desc limit "
				+ begin + "," + pagesize;
		Cursor cursor = db.rawQuery(sql, null);
		TaskInfo task = null;
		TaskListInfo list = null;
		try {
			if (cursor != null && cursor.moveToFirst()) {
				list = new TaskListInfo();
				List<TaskInfo> li = new ArrayList<TaskInfo>();
				for (int i = 0; i < cursor.getCount(); i++) {
					task = new TaskInfo();
					task.setDate(cursor.getString(0));
					task.setName(cursor.getString(1));
					task.setContent(cursor.getString(2));
					task.setState(cursor.getInt(3));
					task.setTemplateID(cursor.getString(4));
					task.setPolId(cursor.getInt(5));
					task.setLon(cursor.getDouble(6));
					task.setLat(cursor.getDouble(7));
					task.setTemplateNum(cursor.getInt(8));
					task.setId(cursor.getInt(9));
					li.add(task);
					cursor.moveToNext();
				}
				list.setTasks(li);
			}
		} catch (Exception e) {
			return null;
		} finally {
			cursor.close();
		}
		return list;
	}
	public TaskListInfo getTaskInfo(int task_id) {
		// begin = 2;
		// end= 6;
		String sql = " select date,name,content,state,templateID,polId,lon,lat,template_num,tid from task_info  where tid="+task_id;
		Cursor cursor = db.rawQuery(sql, null);
		TaskInfo task = null;
		TaskListInfo list = null;
		try {
			if (cursor != null && cursor.moveToFirst()) {
				list = new TaskListInfo();
				List<TaskInfo> li = new ArrayList<TaskInfo>();
				for (int i = 0; i < cursor.getCount(); i++) {
					task = new TaskInfo();
					task.setDate(cursor.getString(0));
					task.setName(cursor.getString(1));
					task.setContent(cursor.getString(2));
					task.setState(cursor.getInt(3));
					task.setTemplateID(cursor.getString(4));
					task.setPolId(cursor.getInt(5));
					task.setLon(cursor.getDouble(6));
					task.setLat(cursor.getDouble(7));
					task.setTemplateNum(cursor.getInt(8));
					task.setId(cursor.getInt(9));
					li.add(task);
					cursor.moveToNext();
				}
				list.setTasks(li);
			}
		} catch (Exception e) {
			return null;
		} finally {
			cursor.close();
		}
		return list;
	}
	public void deleteAttribute(int taskId) {
		db.execSQL("delete from task_info where tid="+taskId);
	}
	public void delete() {
		db.execSQL("delete from task_template_info");
	}
	public int getTaskListCount() {
		String sql = " select date,name,content,state,templateID,polId,lon,lat from task_info ";
		Cursor cursor = db.rawQuery(sql, null);
		TaskInfo task = null;
		TaskListInfo list = null;
		try {
			if (cursor != null && cursor.moveToFirst()) {
				list = new TaskListInfo();
				List<TaskInfo> li = new ArrayList<TaskInfo>();
				list.setTasks(new ArrayList<TaskInfo>());
				for (int i = 0; i < cursor.getCount(); i++) {
					task = new TaskInfo();
					task.setDate(cursor.getString(0));
					task.setName(cursor.getString(1));
					task.setContent(cursor.getString(2));
					task.setState(cursor.getInt(3));
					task.setTemplateID(cursor.getString(4));
					task.setPolId(cursor.getInt(5));
					task.setLon(cursor.getDouble(6));
					task.setLat(cursor.getDouble(7));
					// task.setUpdate(cursor.getString(8));
					li.add(task);
					cursor.moveToNext();
				}
				list.setTasks(li);
			}
		} catch (Exception e) {
			return 0;
		} finally {
			cursor.close();
		}
		if (list == null) {
			return 0;
		}
		return list.getTasks().size();
	}
	public void updateTemplate(int task_id,int num) {
		 ContentValues cv = new ContentValues();
		 cv.put("template_num", num);
		 db.update("task_info", cv,  " tid="+task_id, null);
		 }
}
